<?php
namespace Home\Controller;
use Think\Controller;
class ActivityController extends BaseController {
  
    public function activitylist(){
        //丁红苹 查看活动列表
        $activitymodel=M('activity');//调用表
        $activity=$activitymodel->order('activity_id desc')->select();
        /*dump($goods);*/
        $this->activity=$activity;
        $this->display();
    }



    public function delete($activity_id){
   //丁红苹  删除活动
        $activityModel=M('activity');//调用表
        $res=$activityModel->where("activity_id=$activity_id")->find();
        $photoPath = realpath("./Public/upload/activitypic/".$res['activity_picture']);
        unlink($photoPath);
        //$res=$usersmodel->where("id=$id")->delete();
        if ($activityModel->delete($activity_id)) {
            $this->success('删除成功！');
        } else {
            $this->error('删除失败');
        }
    }



    public function addactivity(){
        $this->display();
    } 




    Public function doactivity(){
        //丁红苹  添加活动
        $data=$_POST;
        $file=$_FILES;
        //dump($file['photo']['size']); 
        //dump($file);      
        $ActivityModel = M('activity'); // 实例化User模型
        $ActivityModel->activity_name =  I('post.activity_name'); // 设置用户的创建时间
        $ActivityModel->activity_data =  I('post.activity_data');
        $ActivityModel->activity_phone =  I('post.activity_phone');
        $ActivityModel->activity_omoney =  I('post.activity_omoney');
        $ActivityModel->activity_ymoney =  I('post.activity_ymoney');
        $ActivityModel->activity_type =  I('post.activity_type');
        $ActivityModel->activity_intro =  I('post.activity_intro');

        $ActivityModel->uploaddate =  date("Y-m-d H:i:s"); // 设置用户的创建时间
        $ActivityModel->activity_address = I('post.activity_address');
         //dump($data);
        if ($file['activity_picture']['size']==0) {//没有上传轮播图片
            //$this->error('请上传背景图片！');                         
        }else{//有图片
            $info=$this->MyUpload();
            $ActivityModel->activity_picture = $info['activity_picture']['savename']; // 保存上传的照片根据需要自行组装
           

        }
        $addresult=$ActivityModel->add(); // 写入用户数据到数据库
        if (false !== $addresult){
            $this->success('上传成功！', '../Activity/activitylist');
        }else{ 
            $this->error('数据错误'); 
        }
        
    }





    public function MyUpload(){
                //上传头像
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize  = 3145728 ;// 设置附件上传大小
        //$upload->allowExts  = array('mp4','jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->exts  = array('jpg', 'gif', 'png', 'jpeg');
        $upload->rootPath  =  './';
        $upload->savePath =  'Public/upload/activitypic/';// 设置附件上传目录

        $upload->autoSub = false;
        $upload->saveName = date("YmdHis").rand(100,999);//定义名字
        // 上传单个文件 
        $info = $upload->upload();                    
            if(!$info) {// 上传错误提示错误信息
                $this->error($upload->getError());
            }else{// 上传成功 获取上传文件信息
                return $info;
            }
    } 





    function editactivity($activity_id){
        //丁红苹  修改活动
        $activity = M('activity');
        $res = $activity->where("activity_id = $activity_id")->find($data);
        $this->list=$res;
        $this->display();
    }




    function doUpdate($activity_id){
   //丁红苹  更新
        $activitymodel=M('activity');
        if($_FILES['activity_picture']['error'] == 0){
            $logoinfo = $this->MyUpload();
            $pname = $logoinfo['file']['savename'];
        } else {
            $pname = "default.jpg";
        }
        $data['activity_name']=I('post.activity_name');
        $data['activity_data']=I('post.activity_data');
        $data['activity_address']=I('post.activity_address');
        $data['activity_phone']=I('post.activity_phone');
        $data['activity_omoney']=I('post.activity_omoney');
        $data['activity_ymoney']=I('post.activity_ymoney');
        $data['activity_type']=I('post.activity_type');
        $data['activity_intro']=I('post.activity_intro');
        // $data['activity_picture']=I('post.activity_picture');
        $data['activity_picture'] = $pname;
        $res=$activitymodel->where("activity_id = $activity_id")->save($data);
        if (false!==$res){
            $this->success('信息修改成功','activitylist');
        } else{
            $this->error('信息修改失败');
        }
    }




 public function activitydetail($activity_id){
       //丁红苹  活动详情
        $activity = M('activity');
        $res = $activity->where("activity_id = $activity_id")->find($data);
        $this->list=$res;
        $this->display();
    
     
    }



}